//const { title } = require("process")

 const app=getApp()
  Page({

    /**
     * 页面的初始数据
     */
    data: {

    },
    onShow(){
      console.log(app.globalData.userInfo)
      this.setData({
        user:app.globalData.userInfo
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
  console.log('用户信息',app.globalData.userInfo)
  console.log(options)
  this.setData({
     goodID:options.id
  })
  //根据传过来的id来查询商品
  wx.cloud.database().collection('shop_goods').doc(options.id).get()
    .then(res=>{
      console.log(res)
      this.setData({
        good:res.data 
    })
  })
  //设置购物车商品数量
  console.log(app.globalData.cartList)
  this.setData({
   cartList:app.globalData.cartList
  })

  },
  //分享给好友 
  onShareAppMessage(){
    return{
      title:this.data.good.title,
      path:'/pages/goodDetail/goodDetail='+this.data.good._id,
      imageUrl:this.data.good.cover
    }
  },
  //分享到朋友圈
  onShareTimeline(){
    return {
      title:this.data.good.title,
      query:{
        id:this.data.good._id
    },
    imageUrl:this.data.good.cover
  }
},
//添加当前商品到购物车
addCart(){
  //登录拦截
  if(!app.globalData.userInfo){
    wx.switchTab({
      url: '/pages/me/me',
      success(){
        wx.showToast({
        icon:'error',
        title:'请登录',
      })
    }
    })

  }
  let carList = app.globalData.cartList
  let index = -1
  if(carList.lengh == 0){
    this.data.good.number = 1
    this.data.good.choose = true  //默认是选中状态
    app.globalData.cartList.push(this.data.good)
    wx.setStorageSync('cartList', app.globalData.cartList)
  }else{
    for(let idx in carList){
      console.log(idx)
      if(carList[idx]._id == this.data.good._id){
        index = idx
      }
    }
    if(index != -1){
      carList[index].number = carList[index].number +1
      app.globalData.cartList = carList
      wx.setStorageSync('cartList', app.globalData.cartList)
 }else{
   this.data.good.number = 1
   this.data.good.choose = true
   //默认是选中状态
   app.globalData.cartList.push(this.data.good)
   wx.setStorageSync('cartList', app.globalData.cartList)
 }
  }
  wx.showToast({
    title:'添加成功!',
  })
  this.setData({
    cartList:app.globalData.cartList
   })

}, 
// toOrder(){
//   let orderList = []
//   orderList.push(this.data.good);
//   app.globalData.orderList = orderList
//   wx.navigateTo({
//     url: '/pages/order2/order2',
//   })
// }, 
toOrder(){
   //登录拦截
   if(!app.globalData.userInfo){
    wx.switchTab({
      url: '/pages/me/me',
      success(){
        wx.showToast({
        icon:'error',
        title:'请登录',
      })
    }
    })

  }
  let orderList = []
  for(let index in this.data.cartList){
    if(this.data.cartList[index].choose == true){
      orderList.push(this.data.cartList[index])
    }
  }
  app.globalData.orderList = orderList
  if(app.globalData.orderList.length == 0){
    wx.showToast({
      icon:'error',
      title: '请选择商品',
    })
    return
  }
  wx.navigateTo({
    url: '/pages/order2/order2',

  })
  // if(result.confirm == true){
  //   wx.switchTab({
  //     url: '/pages/myOrders/myOrders',
  //   })

  //}
 
  
  // wx.navigateBack({
  //   delta:carList,})
},

// clearCarList(){
//   for (let  i in app.globalData.cartList){
//     for(let j in app.globalData.orderList){
//       if(app.globalData.orderList[j]._id == app.globalData.cartList[i]._id){
//       app.globalData.cartList.splice(i,1)
//       }
//     }
//   }
//   wx.setStorageSync('cartList', app.globalData.cartList)
// },
  })